﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string str;
        string tmp;
        public Form1()
        {
            InitializeComponent();
            str = "";//明文
            tmp = "";//密文
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile("D:\\1.txt");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SaveFile("D:\\1.txt");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            str = "";
        }

        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            
            if(((c >='a' && c <= 'z') || (c >= 'A' && c <= 'Z')) && checkBox1.Checked == true)
            {
                str += c;
            
                if ((c >= 'a' && c < 'z') || (c >= 'A' && c < 'Z'))
                {
                    c = (char)(c + 1);
                }
                else if (c == 'z')
                {
                    c = 'a';
                }
                else if (c == 'Z')
                {
                    c = 'A';
                }
                
                tmp += c;
            }
            
            else if ((c >= '0' && c <= '9') && checkBox2.Checked == true)
            {
                str += c;
                tmp += c;
            }
           
            else if(checkBox3.Checked == true)
            {
                str += c;
                tmp += c;
            }
        }

        private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if(radioButton1.Checked == true)
            {
                richTextBox1.Text = tmp;
            }
            else if(radioButton2.Checked == true)
            {
                richTextBox1.Text = str;
            }
            richTextBox1.Select(richTextBox1.Text.Length, 0);//光标置于后面
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton1.Checked == true)
            {
                
                //char a = 'a';
                //foreach (char c in str){
                //    if ((c >= 'a' && c < 'z') || (c >= 'A' && c < 'Z'))
                //    {
                //        a = (char)(c + 1);
                //    }
                //    else if (c == 'z')
                //    {
                //        a = 'a';
                //    }
                //    else if (c == 'Z')
                //    {
                //        a = 'A';
                //    }
                //    tmp += a;
                //}
                richTextBox1.Text = tmp;
            }
           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                richTextBox1.Text = str;
            }
        }

        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            string s = "";
            char a = 'a';
            string s1 = "";
            if(radioButton2.Checked == true)
            {
                label1.Text = "选择部分转密文：";
                s = richTextBox1.SelectedText;
                foreach (char c in s)
                {
                    if ((c >= 'a' && c < 'z') || (c >= 'A' && c < 'Z'))
                    {
                        a = (char)(c + 1);
                        s1 += a;
                    }
                    else if (c == 'z')
                    {
                        a = 'a';
                        s1 += a;
                    }
                    else if (c == 'Z')
                    {
                        a = 'A';
                        s1 += a;
                    }
                    else
                    {
                        s1 += c;
                    }
                }
                label2.Text = s1;
            }
            else if (radioButton1.Checked == true)
            {
                label1.Text = "选择部分转明文：";
                s = richTextBox1.SelectedText;
                foreach (char c in s)
                {
                    if ((c > 'a' && c <= 'z') || (c > 'A' && c <= 'Z'))
                    {
                        a = (char)(c - 1);
                        s1 += a;
                    }
                    else if (c == 'a')
                    {
                        a = 'z';
                        s1 += a;
                    }
                    else if (c == 'A')
                    {
                        a = 'Z';
                        s1 += a;
                    }
                    else
                    {
                        s1 += c;
                    }
                }
                label2.Text = s1;
            }
        }
    }
}
